using System;
using System.Collections.Generic;
using System.Text;
using Spacedash.Game.Cameras;
using Barrage;
using SlimDX;
using Spacedash.Game.Nodes;
using Spacedash.Game.Items;
using Spacedash.Logic.Scenario;
using Spacedash.Logic;
using Barrage.Items;
using Barrage.Meshes;
using Barrage.Textures;
using Barrage.Elements;
using Barrage.Elements.Cameras;
using Barrage.Audios;
using Barrage.Global;

namespace Spacedash.Game.Scenes
{
    /// <summary>
    /// Scene that shows all the 3D elements of a race: Ships, scenario, skybox..
    /// </summary>
    public class RaceScene : Scene
    {
        Race race;
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="race">Race instance</param>
        /// <param name="viewportSize">Viewport size in pixels</param>
        public RaceScene(Race race,Vector2 viewportSize)
        {
            this.race = race;

            addDefaultElements();

            loadScenario(viewportSize);
        }

        ScenarioNode scenarioItem;
        List<ShipNode> shipNodes=new List<ShipNode>(4);
        JukeboxItem jukebox;

        List<RaceCamera> cameras = new List<RaceCamera>(4);
        /// <summary>
        /// List of RaceCameras in the scene (there are 4 cameras, one for each player)
        /// </summary>
        public List<RaceCamera> Cameras { get { return cameras; } }

        /// <summary>
        /// Adds some lighting information and a skybox to the scene
        /// </summary>
         void addDefaultElements(){
            Light l = new Light(new Color3(1, 0, 0));
            l.Motion.Translation = new Vector3(100, 100, 100);
            l.Attenuation = new Vector3(1, 0.01f, 0);
            l.Specular =new Color3(1,0,0);
            addLight(l);
        
            
            Item i = new Skybox("Cube.dds"); 
            i.Motion.Scale = new Vector3(100);
            Add(i);
            
        }


         void loadScenario(Vector2 viewportSize){
            //Ship s = new Ship(scenario);
            //s.Motion.Translation = new Vector3(0, 2, 2);
            //Add(s);
            scenarioItem = new ScenarioNode(race.Scenario);
            Add(scenarioItem);
            //Add(new Item(new ParticleMesh(TextureManager.Textures["Spark.dds"])));
            foreach (Ship s in race.Ships)
            {
                ShipNode shipNode = new ShipNode(s);
                RaceCamera rc = new RaceCamera(shipNode,viewportSize);
                cameras.Add(rc);
                shipNodes.Add(shipNode);
                Add(shipNode);
                Add(rc);
            }
            jukebox = new JukeboxItem();
            Add(jukebox);
        }

        /// <summary>
        /// \internal Updates items inside the scene and does some camera logic
        /// </summary>
        public override void Update()
        {
            for (int i=0;i<cameras.Count;i++)
            {
                if (Gamepad.Instance((SlimDX.XInput.UserIndex)i).ButtonPressed(SlimDX.XInput.GamepadButtonFlags.Y)
                    || (Keyboard.Instance.KeyPressed(SlimDX.DirectInput.Key.C)))
                    cameras[i].Next();
            }

            if (Keyboard.Instance.KeyPressed(SlimDX.DirectInput.Key.M))
                jukebox.Jukebox.SetNextSong();

            for (int i = 0; i < race.PlayerNumber; i++)
            {
                Matrix mat = Matrix.Translation(new Vector3(0, 0, -2.5f)) * Matrix.LookAtLH(race.Ships[i].Motion.Translation + shipNodes[i].Ship.Normal * 2, race.Ships[i].Motion.Translation, shipNodes[i].Ship.Tangent) * Matrix.OrthoLH(7, 7, 0.1f, 100);
                this.scenarioItem.ScenarioItem.Mesh.FirstMaterial.MatrixParameters[i] = mat;
            }
            base.Update();      
        }
    }
}
